package com.lambert.ginkgo.controller;

import com.lambert.ginkgo.service.OpenAIService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * OpenAI 接口控制器，对外暴露API
 */
@RestController
@RequestMapping("/api/openai")
public class OpenAIController {

    private final OpenAIService openAIService;

    public OpenAIController(OpenAIService openAIService) {
        this.openAIService = openAIService;
    }

    /**
     * 同步聊天接口（一次性返回结果）
     * @param request 用户请求，包含message字段
     * @return 模型回复
     */
    @PostMapping("/chat")
    public Mono<String> chat(@RequestBody ChatRequest request) {
        return openAIService.chat(request.getMessage());
    }

    /**
     * 流式聊天接口（实时返回结果片段）
     * 注意：使用 text/event-stream 媒体类型支持SSE
     */
    @PostMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> streamChat(@RequestBody ChatRequest request) {
        return openAIService.streamChat(request.getMessage());
    }

    // 简单的请求DTO
    public static class ChatRequest {
        private String message;

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
}
